package demo;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.Date;

/**
 * 新Api 日期 时间 demo
 *
 * @author dingwen
 * 2021.06.09 14:58
 */
public class DataTimeDemo {
    public static void main(String[] args) {
        // LocalDate.now() = 2021-06-09
        System.out.println("LocalDate.now() = " + LocalDate.now());

        //LocalTime.now() = 15:00:27.951
        System.out.println("LocalTime.now() = " + LocalTime.now());

        //LocalDateTime.now() = 2021-06-09T15:00:27.950
        System.out.println("LocalDateTime.now() = " + LocalDateTime.now());


        //LocalDate.of(2020,8,29) = 2020-08-29
        System.out.println("LocalDate.of(2020,8,29) = " + LocalDate.of(2020, 8, 29));

        //LocalTime.of(15,6,25,655) = 15:06:25.000000655
        System.out.println("LocalTime.of(15,6,25,655) = " + LocalTime.of(15, 6, 25, 655));

        //LocalDateTime.of(2016,7,10,16,54,55) = 2016-07-10T16:54:55
        System.out.println("LocalDateTime.of(2016,7,10,16,54,55) = " + LocalDateTime.of(2016, 7, 10, 16, 54, 55));


        LocalDateTime now = LocalDateTime.now();

        //now.plusYears(1L) = 2022-06-09T15:20:17.968
        System.out.println("now.plusYears(1L) = " + now.plusYears(1L));

        //now.plusDays(1l) = 2021-06-10T15:20:17.968
        System.out.println("now.plusDays(1l) = " + now.plusDays(1L));

        //now.plusMonths(30L) = 2023-12-09T15:20:17.968
        System.out.println("now.plusMonths(30L) = " + now.plusMonths(30L));

        //now.plusHours(2L) = 2021-06-09T17:20:17.968
        System.out.println("now.plusHours(2L) = " + now.plusHours(2L));

        //now.plusMinutes(10L) = 2021-06-09T15:30:17.968
        System.out.println("now.plusMinutes(10L) = " + now.plusMinutes(10L));


        //now.plusSeconds(10) = 2021-06-09T15:20:27.968
        System.out.println("now.plusSeconds(10) = " + now.plusSeconds(10L));

        //now.plus(3L, ChronoUnit.HOURS) = 2021-06-09T18:20:17.968
        System.out.println("now.plus(3L, ChronoUnit.HOURS) = " + now.plus(3L, ChronoUnit.HOURS));

        //now.plus(-1L,ChronoUnit.YEARS) = 2020-06-09T15:20:17.968
        System.out.println("now.plus(-1L,ChronoUnit.YEARS) = " + now.plus(-1L, ChronoUnit.YEARS));


        // 这个月的第一天
        //now.withDayOfMonth(1) = 2021-06-01T15:27:06.641
        System.out.println("now.withDayOfMonth(1) = " + now.withDayOfMonth(1));

        // 这年中的第100天
        //now.withDayOfYear(100) = 2021-04-10T15:27:06.641
        System.out.println("now.withDayOfYear(100) = " + now.withDayOfYear(100));


        // 指定年份
        //now.withYear(2022) = 2022-06-09T15:27:06.641
        System.out.println("now.withYear(2022) = " + now.withYear(2022));

        // 指定月份
        //now.withMonth(2) = 2021-02-09T15:27:06.641
        System.out.println("now.withMonth(2) = " + now.withMonth(2));


        //now.getDayOfMonth() = 9
        System.out.println("now.getDayOfMonth() = " + now.getDayOfMonth());

        //now.getDayOfWeek() = WEDNESDAY
        System.out.println("now.getDayOfWeek() = " + now.getDayOfWeek());

        //now.getDayOfYear() = 160
        System.out.println("now.getDayOfYear() = " + now.getDayOfYear());

        //now.getHour() = 15
        System.out.println("now.getHour() = " + now.getHour());

        //now.getSecond() = 30
        System.out.println("now.getSecond() = " + now.getSecond());

        //now.getMonth() = JUNE
        System.out.println("now.getMonth() = " + now.getMonth());

        //now.getMonthValue() = 6
        System.out.println("now.getMonthValue() = " + now.getMonthValue());


        LocalDateTime localDateTime1 = LocalDateTime.now();
        LocalDateTime localDateTime2 = LocalDateTime.now().plusHours(1L);

        //localDateTime1.isAfter(localDateTime2) = false
        System.out.println("localDateTime1.isAfter(localDateTime2) = " + localDateTime1.isAfter(localDateTime2));

        //localDateTime2.isBefore(localDateTime1) = false
        System.out.println("localDateTime2.isBefore(localDateTime1) = " + localDateTime2.isBefore(localDateTime1));


        LocalDate localDate = LocalDate.now();
        // 判断是否为闰年
        //localDate.isLeapYear() = false
        System.out.println("localDate.isLeapYear() = " + localDate.isLeapYear());


        //返回当前时间，根据系统时间和UTC
        //Clock.systemUTC() = SystemClock[Z]
        System.out.println("Clock.systemUTC() = " + Clock.systemUTC());


        Instant instant = Instant.now();
        //instant = 2021-06-09T07:47:15.247Z
        System.out.println("instant = " + instant);


        Date date = new Date(System.currentTimeMillis());
        //date = Wed Jun 09 15:47:15 CST 2021
        System.out.println("date = " + date);

        // instant -> date
        Date date1 = Date.from(instant);
        //date1 = Wed Jun 09 15:50:16 CST 2021
        System.out.println("date1 = " + date1);

        // date -> instant
        Instant instant1 = date1.toInstant();
        //instant1 = 2021-06-09T07:50:16.285Z
        System.out.println("instant1 = " + instant1);


        LocalDate localDate1 = LocalDate.of(2019, 10, 10);
        LocalDate localDate2 = LocalDate.of(2018, 3, 13);
        //localDate1 - localDate2
        Period period = Period.between(localDate2, localDate1);

        //period.getYears() = 1
        System.out.println("period.getYears() = " + period.getYears());

        //period.getMonths() = 6
        System.out.println("period.getMonths() = " + period.getMonths());

        //period.getDays() = 27
        System.out.println("period.getDays() = " + period.getDays());

        //period.get(ChronoUnit.YEARS) = 1
        System.out.println("period.get(ChronoUnit.YEARS) = " + period.get(ChronoUnit.YEARS));


        LocalTime localTime1 = LocalTime.of(6, 3, 33);
        LocalTime localTime2 = LocalTime.of(22, 22, 22);

        //localDateTime2 - localDateTime1
        Duration duration = Duration.between(localDateTime1, localDateTime2);

        //duration.toDays() = 0
        System.out.println("duration.toDays() = " + duration.toDays());

        //duration.toHours() = 1
        System.out.println("duration.toHours() = " + duration.toHours());

        //duration.getNano() = 0
        System.out.println("duration.getNano() = " + duration.getNano());

        //duration.toMinutes() = 60
        System.out.println("duration.toMinutes() = " + duration.toMinutes());


        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.BASIC_ISO_DATE;
        String format = LocalDateTime.now().format(dateTimeFormatter);

        //format = 20210609
        System.out.println("format = " + format);


        DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        String format1 = LocalDateTime.now().format(dateTimeFormatter1);

        //format1 = 2021/06/09 16:20:0
        System.out.println("format1 = " + format1);

        //2018-01-13 21:27:30 对应 yyyy-MM-dd HH:mm:ss
        //20180113213328 对应 yyyyMMddHHmmss
        String datetime =  "2018-01-13 21:27:30";
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime ldt = LocalDateTime.parse(datetime, dtf);
        System.out.println(ldt);


    }
}
